package com.pubinfo.passbook.common.service.mss.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.pubinfo.passbook.common.config.ApplicationConfig;
import com.pubinfo.passbook.common.config.ProfileConfig;
import com.pubinfo.passbook.common.entity.*;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.mapper.*;
import com.pubinfo.passbook.common.model.bo.eip.ReceiveTaskParam;
import com.pubinfo.passbook.common.model.bo.uccp.ShortMessageParam;
import com.pubinfo.passbook.common.model.bo.witeoff.CityInfo;
import com.pubinfo.passbook.common.model.bo.witeoff.InvoiceInfo;
import com.pubinfo.passbook.common.model.bo.witeoff.MedicineOnJobOrRetireBO;
import com.pubinfo.passbook.common.model.bo.witeoff.WiteoffBase;
import com.pubinfo.passbook.common.model.bo.witeoff.subitem.MedicineOnJobOrRetireSubitem;
import com.pubinfo.passbook.common.model.constant.StaffStatusEnum;
import com.pubinfo.passbook.common.model.dto.witeoff.MedicineOnJobOrRetireDTO;
import com.pubinfo.passbook.common.model.dubbo.ESBServiceData;
import com.pubinfo.passbook.common.model.http.baseEntity.BaseBody;
import com.pubinfo.passbook.common.model.http.baseEntity.Baseinfo;
import com.pubinfo.passbook.common.model.http.baseEntity.Message;
import com.pubinfo.passbook.common.model.http.baseEntity.request.IRequest;
import com.pubinfo.passbook.common.model.http.baseEntity.request.MessageList;
import com.pubinfo.passbook.common.model.http.baseEntity.request.RequestEnvelope;
import com.pubinfo.passbook.common.model.http.request.AutoCreateWriteoff.AutoCreateWriteoffBody;
import com.pubinfo.passbook.common.model.http.request.AutoCreateWriteoff.AutoCreateWriteoffMessageList;
import com.pubinfo.passbook.common.model.http.request.GetSapNumber.GetSapNumberBody;
import com.pubinfo.passbook.common.model.http.request.GetSapNumber.GetSapNumberItems;
import com.pubinfo.passbook.common.model.http.response.GetAutoCreateWriteoff.WriteoffEnvelope;
import com.pubinfo.passbook.common.model.http.response.GetAutoCreateWriteoff.WriteoffMessage;
import com.pubinfo.passbook.common.model.ruler.PayInformation;
import com.pubinfo.passbook.common.model.ruler.Reimbursement;
import com.pubinfo.passbook.common.model.ruler.ReimbursementParam;
import com.pubinfo.passbook.common.model.ruler.UserAcc;
import com.pubinfo.passbook.common.model.vo.RuleAuditVo;
import com.pubinfo.passbook.common.model.vo.base.JsonResult;
import com.pubinfo.passbook.common.model.vo.witeoff.WiteoffResultCode;
import com.pubinfo.passbook.common.service.aviator.AviatorScriptService;
import com.pubinfo.passbook.common.service.dedicated.DedicatedService;
import com.pubinfo.passbook.common.service.dubbo.DubboService;
import com.pubinfo.passbook.common.service.master.*;
import com.pubinfo.passbook.common.service.mss.HttpService;
import com.pubinfo.passbook.common.service.ruler.CheckService;
import com.pubinfo.passbook.common.service.ruler.InformationService;
import com.pubinfo.passbook.common.service.ruler.RuleShengGongSiService;
import com.pubinfo.passbook.common.service.ruler.RuleWenZhouService;
import com.pubinfo.passbook.common.utils.UccpUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;


@Slf4j
@Service("httpServiceWithEngineImpl")
public class HttpServiceWithEngineImpl extends HttpServiceImpl implements HttpService {

    @Autowired
    @Qualifier("ruleEngineCheckServiceImpl")
    CheckService ruleEngineCheckService;

    @Override
    public List<TPassbook> sendTo(MedicineOnJobOrRetireDTO jobDTO, Boolean isShow) {
        BigDecimal sum = ruleEngineCheckService.checkPassbookSum(jobDTO);
        List<TPassbook> passbookList = passbookService.getPassbookById(jobDTO.getInvoiceIds());
        TPassbook passbook = passbookList.get(0);
        String idNo = passbook.getPayerPartyCode(); //报账人的身份证号
        String acct = dubboService.getPersonsDetailByAcct(idNo);
        ESBServiceData.CertDetail certDetail = dubboService.getPersonsDetailByIdNo(acct);
        jobDTO.setNsrsbh(passbook.getNsrsbh());
        List<TRuleUserWiteoff> witeoffs = TRuleUserWiteoff.toList(jobDTO, acct);
        if (sum.compareTo(BigDecimal.ZERO) == 1) { //为0,则不进行报销.
            jobDTO.setSum(sum);
            //jobDTO.setpi
            if (!isTest) { //测试时不保存
                witeoffService.saveBatch(witeoffs); //保存报账用户金额
            }
        }
//        else {
//            ShortMessageParam param = new ShortMessageParam("您好！您于" + UccpUtil.getSubmitDate(passbookList) + "提交单位报销的医药费票据共" + passbookList.size() + "张，根据补助政策核定报销金额为" + sum + "元。", certDetail.getStaffPhone());
//                vatService.sendShortMessage(param);
//            return JsonResult.error(WiteoffResultCode.NOT_REIMBURSEMENT_STANDARD);
//            return passbookList;
//          }

        String areaName = certDetail.getSubsidiaryName();
        String sapCompayCode = sourceInvoiceMapper.selectCompanyType(areaName);
        jobDTO.setPhone(certDetail.getStaffPhone());
        jobDTO.setIdNo(idNo);
        jobDTO.setSapCompayCode(sapCompayCode);
        jobDTO.setWiteoffs(witeoffs);
        setAttchmentAddsObj(jobDTO, idNo);
        Boolean isGenus = genusService.isGenus(idNo);
        String staffCode = ruleService.getStaffCode(idNo, isGenus);
        UserAcc userAcc = ruleService.getUserAcc(staffCode, isShow);
        jobDTO.setUserAcc(userAcc);
        setPayMentItems(jobDTO, staffCode);
        MedicineOnJobOrRetireBO bo = new MedicineOnJobOrRetireBO(jobDTO);
        bo = setJobOrRetireDTOId(bo);
        log.info(JSONObject.toJSONString(bo));
        Integer status = StaffStatusEnum.getEnumByStatus(certDetail.getStaffStatus()).getCode();
        MedicineOnJobOrRetireSubitem lineItem = new MedicineOnJobOrRetireSubitem(profileConfig.getActiveProfile(), sum, status, isGenus, sapCompayCode, userAcc);
        lineItem.setOtherSystemDetailId(UUID.randomUUID().toString());
        List<MedicineOnJobOrRetireSubitem> lineItems = new ArrayList<>();
        lineItems.add(lineItem);
        bo.setLineItems(lineItems);
        //暂时注释
        postXmlAndSave(bo, JSONObject.parseObject(JSON.toJSONString(jobDTO)), true);
        return new ArrayList<>();

    }
}
